package com.example.demo.controller;



import java.io.*;
import java.util.Map;

import com.example.demo.controller.ResourceLoader;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;



//html 的生成器

public class Html {


    public static void main(String[] args) throws Exception {

        // classpath 中模板路径
        String temp = "temple/overseaAssistance.html";
        // classpath 路径
        String outputFileClass = ResourceLoader.getPath("");
        System.out.println(outputFileClass);
        String ss = new File(outputFileClass).getParentFile().getParent();
        System.out.println(ss);

//         String htmlStr = HtmlGenerator.generate(template, variables);
//           System.out.println(htmlStr);

        // 生成pdf路径
//         outputFile = outputFile == null ? new File(outputFileClass).getParentFile().getParent()+ "/tmp/"+ System.currentTimeMillis() + ".pdf" : outputFile;



        // 生成pdf路径
//           OutputStream out = new FileOutputStream(outputFile);
//           PdfDocumentGenerator.pdfgenerate(htmlStr, out);
    }
    /**
     *
     * 根据上面的main 方法抽出的通用方法
     * 传入模版路径 和数据生成html 返回html 的内容
     * template 模版的路径 variables数据
     */
    public static String generate(String template, Map<String,Object> variables) throws IOException, TemplateException{

        Configuration config = FreemarkerConfiguration.getConfiguation();
        //Template tp = configuration.getTemplate("overseaAssistance.html");
        Template tp = config.getTemplate(template);
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        tp.setEncoding("UTF-8");
        tp.process(variables, writer);
        String htmlStr = stringWriter.toString();
        writer.flush();
        writer.close();
        return htmlStr;
    }
}

